package com.juyu.miliao.api.dao.mysql;

import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.Visitor;
import com.juyu.miliao.common.dataSource.tool.distributeOperate.DistributeOperate;
import com.juyu.miliao.common.dataSource.tool.operate.Operate;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;

@Repository
public class UserInfoDao {
    @Autowired
    private SqlSession sqlSession;

    @Operate("strategy('byUserId', #id)")
    public User getUserInfoById(long id) {
        return sqlSession.selectOne("getUserInfoById", id);
    }

    @Operate("strategy('byUserId', #id)")
    public User getUserById(long id) {
        return sqlSession.selectOne("getUserById", id);
    }

    @Operate("strategy('byUserId', #entity.userId)")
    public int updateUserInfo(User entity) {
        return sqlSession.update("updateUserInfo", entity);
    }

    //查默认库
    public List<Visitor> getVisitorIdsByUserId(int id, int startNum, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("startNum", startNum);
        params.put("pageSize", pageSize);
        return sqlSession.selectList("getVisitors", params);
    }

    @DistributeOperate(value = "strategy('byUserId', #userIds)")
    public List<User> getVisitorListByUserId(Collection<Long> userIds) {
        return sqlSession.selectList("getVisitorListByUserIds", Collections.singletonMap("userIds", userIds));
    }

    /**
     * 不适用,直接更新了,过期策略是什么,现在改成每次的取的时候判断
     */
    @Operate("strategy('byUserId', #userId)")
    public int updateUserIsVip(int isVip, int vipPayType, int vipLevel, long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("isVip", isVip);
        params.put("userId", userId);
        params.put("vipPayType", vipPayType);
        params.put("vipLevel", vipLevel);
        return sqlSession.update("updateUserIsVip", params);
    }

    @DistributeOperate(value = "strategy('byUserId', #userIds)", resultHandler = "sortByUserIds(#userIds)")
    public List<User> getHotUserList(List<Long> userIds) {
        return sqlSession.selectList("getHotUserList", Collections.singletonMap("userIds", userIds));
    }

/*	@DistributeOperate(value = "strategy('byUserId', #userIds)")
	public List<User> getNewUserList(List<Integer> userIds){
		return sqlSession.selectList("getNewUserList",  Collections.singletonMap("userIds", userIds));
	}

	@DistributeOperate(value = "strategy('byUserId', #userIds)")
	public List<User> getNotBuyUserList(List<Integer> userIds){
		return sqlSession.selectList("getNotBuyUserList", Collections.singletonMap("userIds", userIds));
	}*/

    @DistributeOperate(value = "strategy('byUserId', #userIds)", resultHandler = "sortByUserIds(#userIds)")
    //@DistributeOperate(value = "strategy('byUserId', #userIds)")
    //@DistributeOperate(ds = "miliao-user")
    public List<User> getUserListByUserIds(List<Long> userIds) {
        return sqlSession.selectList("getUserListByUserIds", Collections.singletonMap("userIds", userIds));
    }

    //最热
    @DistributeOperate(ds = "miliao-user")
    public List<User> getHotSortUserList() {
        return sqlSession.selectList("getHotSortUserList");
    }

    //最新
    @DistributeOperate(ds = "miliao-user")
    public List<User> getNewSortUserList() {
        LocalDate date = LocalDate.now().minus(1, ChronoUnit.MONTHS);
        String dates = date.toString();
        return sqlSession.selectList("getNewSortUserList", dates);
    }

    //免费
    @DistributeOperate(ds = "miliao-user")
    public List<User> notBuySortUserList() {
        return sqlSession.selectList("getNotBuySortUserList");
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getUserListRand(int randNum, List<Long> userIds) {
        Map<String, Object> params = new HashMap<>();
        params.put("list", userIds);
        params.put("randNum", randNum);
        return sqlSession.selectList("getUserListRand", params);
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getCommonUserListByUserIds(List<Long> userIds) {
        return sqlSession.selectList("getCommonUserListByUserIds", Collections.singletonMap("userIds", userIds));
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getQualityHostByUserIds(List<Long> userIds) {
        return sqlSession.selectList("getQualityHostByUserIds", Collections.singletonMap("userIds", userIds));
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getHostLineByFree(List<Long> userIds) {
        return sqlSession.selectList("getHostLineByFree", Collections.singletonMap("userIds", userIds));
    }


    public int updateUserLoginNickName(long userId, String nickName) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("nickName", nickName);
        return sqlSession.update("updateUserLoginNickName", params);
    }

    @Operate("strategy('byUserId', #userId)")
    public int updateCleanUserInfo(long userId) {
        return sqlSession.update("updateCleanUserInfo", userId);
    }

    @Operate("strategy('byUserId', #userId)")
    public User getUserStatus(Long userId) {
        return sqlSession.selectOne("getUserStatus", userId);
    }

    @DistributeOperate(ds = "miliao-user")
    public List<Long> getSameCityUser(String city) {
        List<Long> result = new ArrayList<>();
        List<User> list = sqlSession.selectList("getSameCityUser", city);
        if (list != null) {
            for (User user : list) {
                result.add(user.getUserId());
            }
        }
        return result;
    }


    /**
     * todo: sql性能问题
     * 获得随机主播,注:分库要去掉多余的
     *
     * @param ids 排除的主播id
     * @param max 最大数量
     * @return 主播userId
     */
    @DistributeOperate(ds = "miliao-user")
    public List<Long> getRandomHostUser(Set<Long> ids, int max) {
        List<Long> result = new ArrayList<>();
        Map<String, Object> map = new HashMap<>(4);
        if (ids != null && ids.isEmpty()) {
            ids = null;
        }
        map.put("ids", ids);
        map.put("max", max);
        List<User> list = sqlSession.selectList("getRandomHostUser", map);
        if (list != null) {
            for (User user : list) {
                result.add(user.getUserId());
            }
        }
        return result;
    }

    /**
     * 获取性别
     *
     * @param userId userId
     * @return Boolean  false  null 男   true- 女
     */
    @Operate("strategy('byUserId', #userId)")
    public Boolean getUserSex(Long userId) {
        Integer integer = sqlSession.selectOne("getUserSex", userId);
        //1男 2女
        return integer != null && integer != 1;
    }


    // @DistributeOperate(ds = "miliao-user")
    @Operate("strategy('byUserId', #userId)")
    public int updateUserInfoByUserCoverUrl(long userId, String userCoverUrl) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("userCoverUrl", userCoverUrl);
        return sqlSession.update("updateUserInfoByUserCoverUrl", params);
    }

    /**
     * 获取优质主播id列表
     *
     * @return
     */
    @DistributeOperate(ds = "miliao-user")
    public List<Long> getHostUserList() {
        return sqlSession.selectList("getHostUserList");
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getHostList() {
        return sqlSession.selectList("getHostList");
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getHostUserListByIds(List<Long> userIds) {
        return sqlSession.selectList("getHostUserListByIds",
                Collections.singletonMap("userIds", userIds));
    }

    /**
     * 根据用户设置的语音当前金币价格 修改用户聊天价格
     *
     * @param goldPrice
     * @param userId
     */
    @Operate("strategy('byUserId', #userId)")
    public void updateUserInfoByVoiceGoldPrice(String goldPrice, long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("goldPrice", goldPrice);
        sqlSession.update("updateUserInfoByVoiceGoldPrice", params);

    }

    /**
     * 根据用户设置的视频当前金币价格 修改用户视频价格
     *
     * @param goldPrice
     * @param userId
     */
    @Operate("strategy('byUserId', #userId)")
    public void updateUserInfoByVideoGoldPrice(String goldPrice, long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("goldPrice", goldPrice);
        sqlSession.update("updateUserInfoByVideoGoldPrice", params);
    }

    @Operate("strategy('byUserId', #user.userId)")
    public void updateUserCertificationHost(User user) {
        sqlSession.update("updateUserCertificationHost", user);
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getWarnManUserList(long userId) {
        return sqlSession.selectList("getWarnManUserList", userId);
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getQualityOnLineUserByUserIds(List<Long> cids) {
        return sqlSession.selectList("getQualityOnLineUserByUserIds", Collections.singletonMap("userIds", cids));
    }


    @DistributeOperate(ds = "miliao-user")
    public List<User> getSameCityOnLineUserByUserIds(List<String> cids) {
        return sqlSession.selectList("getSameCityOnLineUserByUserIds", Collections.singletonMap("userIds", cids));
    }

    @Operate("strategy('byUserId', #userId)")
    public int updateUserVipLevel(long userId, int vipLevel) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("vipLevel", vipLevel);
        return sqlSession.update("updateUserVipLevel", params);
    }

    /**
     * 更新用户地址
     *
     * @param userId
     * @param city
     * @param province
     */
    @Operate("strategy('byUserId', #userId)")
    public int updateUserLocation(long userId, String city, String province, String location) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("userId", userId);
        params.put("city", city);
        params.put("province", province);
        params.put("location", location);
        return sqlSession.update("updateUserLocation", params);

    }

    @Operate("strategy('byUserId', #userId)")
    public List<User> getSameCityOnLineUserByUserId(long userId) {
        return sqlSession.selectList("getSameCityOnLineUserByUserId", userId);

    }

    @Operate("strategy('byUserId', #userId)")
    public User getUserIconByUserId(long userId) {
        return sqlSession.selectOne("getUserIconByUserId", userId);
    }

    //User表:first_vip   tinyint(1)   是否为vip,历史冲过1次vip的就算vip,(0-false 1-true)
    @Operate("strategy('byUserId', #userId)")
    public Boolean getUserFirstVip(Long userId) {
        return sqlSession.selectOne("getUserFirstVip", userId);
    }


    //获取主播用户信息
    @DistributeOperate(ds = "miliao-user")
    public List<User> getUserForLabelByUserIds(List<Long> userIdsList) {

        return sqlSession.selectList("getUserForLabelByUserIds", Collections.singletonMap("userIds", userIdsList));
    }

    @Operate("strategy('byUserId', #userId)")
    public void updateIcon(long userId, String iconUrl, int iconType, int userIcon) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("userId", userId);
        params.put("iconUrl", iconUrl);
        params.put("iconType", iconType);
        params.put("iconId", userIcon);

        sqlSession.update("updateIcon", params);

    }

    @Operate("strategy('byUserId', #userId)")
    public int updateNiceName(long userId, String nickName) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("userId", userId);
        params.put("nickName", nickName);
        return sqlSession.update("updateNiceName", params);
    }

    /**
     * 更新用户是否为svip
     *
     * @param isSVip 1：是，2：不是
     * @param userId
     */
    @Operate("strategy('byUserId', #userId)")
    public void updateUserIsSVip(int isSVip, long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("isSVip", isSVip);
        params.put("userId", userId);
        sqlSession.update("updateUserIsSVip", params);
    }

    @Operate("strategy('byUserId', #userId)")
    public User selectAdminByUnionId(long userId) {
        return sqlSession.selectOne("selectAdminByUnionId", userId);
    }

    @Operate("strategy('byUserId', #userId)")
    public void updateUserInfoVehicleId(long userId, int vehicleId) {
        Map<String, Object> params = new HashMap<>();
        params.put("vehicleId", vehicleId);
        params.put("userId", userId);
        sqlSession.update("updateUserInfoVehicleId", params);
    }

    @Operate("strategy('byUserId', #userId)")
    public void updateVideoPrice(long userId, int goldCoinsPerMinute) {
        Map<String, Object> params = new HashMap<>();
        params.put("goldCoinsPerMinute", goldCoinsPerMinute);
        params.put("userId", userId);
        sqlSession.update("updateChatPrice", params);
    }

    @DistributeOperate(ds = "miliao-user")
    public List<User> getHostUserInfoList() {
        return sqlSession.selectList("getHostUserInfoList");
    }

    @DistributeOperate(ds = "miliao-user")
    public List<Long> getTestUserList() {
        return sqlSession.selectList("getTestUserList");
    }


    /**
     * 更新用户是否为铂金vip
     *
     * @param isPlatinumVip 1：是，2：不是
     * @param userId
     */
    @Operate("strategy('byUserId', #userId)")
    public void updateUserIsPlatinumVip(int isPlatinumVip, long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("isPlatinumVip", isPlatinumVip);
        params.put("userId", userId);
        sqlSession.update("updateUserIsPlatinumVip", params);
    }
}
